package com.situ.company.employee.servlet;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.employee.service.IEmployeeService;
import com.situ.company.employee.service.impl.EmployeeServiceImpl;
import com.situ.company.util.FmtRequest;
import com.situ.company.util.Upload;

@WebServlet("/EmployeeUploadServlet")
public class EmployeeUploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	IEmployeeService service=new EmployeeServiceImpl();

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, Object> map = Upload.upload(request);
		List<String> list = (List<String>)map.get("list");
		String image=list.get(0);
		String code=(String)map.get("code");
		
		EmployeeModel model=new  EmployeeModel(code);
		model.setImage(image);
		String res=service.update(model);
		
		//返回数据
		Map<String,String> result=new HashMap<>();
		result.put("code", res);
		result.put("image", image);
		FmtRequest.write(response.getWriter(), result);
		
	}

	

}
